package com.budwk.app.miniapp.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.miniapp.models.WxMina;
import com.budwk.app.miniapp.services.WxminaService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/miniapp/mina")
@ApiDefinition(tag = "微信小程序-mina")
@Slf4j
public class WxminaController {
    @Inject
    private WxminaService wxMinaService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询WxMina")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = WxMina.class
    )
    @ApiResponses(
            implementation = WxMina.class
    )
    @SaCheckLogin
    public Result<?> list(@Param("..") WxMina bean, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }

        if (bean.getAppname() != null && Strings.isNotBlank(bean.getAppname().toString())) {
            cnd.and("appname", "=", bean.getAppname());
        }

        if (bean.getAppid() != null && Strings.isNotBlank(bean.getAppid().toString())) {
            cnd.and("appid", "=", bean.getAppid());
        }

        if (bean.getAppsecret() != null && Strings.isNotBlank(bean.getAppsecret().toString())) {
            cnd.and("appsecret", "=", bean.getAppsecret());
        }

        if (bean.getWxid() != null && Strings.isNotBlank(bean.getWxid().toString())) {
            cnd.and("wxid", "=", bean.getWxid());
        }

        if (bean.getMchid() != null && Strings.isNotBlank(bean.getMchid().toString())) {
            cnd.and("mchid", "=", bean.getMchid());
        }

        if (bean.getCreatedBy() != null && Strings.isNotBlank(bean.getCreatedBy().toString())) {
            cnd.and("createdby", "=", bean.getCreatedBy());
        }

        if (bean.getCreatedAt() != null && Strings.isNotBlank(bean.getCreatedAt().toString())) {
            cnd.and("createdat", "=", bean.getCreatedAt());
        }

        if (bean.getUpdatedBy() != null && Strings.isNotBlank(bean.getUpdatedBy().toString())) {
            cnd.and("updatedby", "=", bean.getUpdatedBy());
        }

        if (bean.getUpdatedAt() != null && Strings.isNotBlank(bean.getUpdatedAt().toString())) {
            cnd.and("updatedat", "=", bean.getUpdatedAt());
        }

        if (bean.getDelFlag() != null && Strings.isNotBlank(bean.getDelFlag().toString())) {
            cnd.and("delflag", "=", bean.getDelFlag());
        }

        return Result.data(wxMinaService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增WxMina")
    @ApiFormParams(
            implementation = WxMina.class
    )
    @ApiResponses
    @SLog(tag = "新增WxMina", value = "${args[0].id}")
    @SaCheckLogin
    public Result<?> create(@Param("..") WxMina wxMina, HttpServletRequest req) {
        wxMina.setCreatedBy(SecurityUtil.getUserId());
        wxMinaService.insert(wxMina);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改WxMina")
    @ApiFormParams(
            implementation = WxMina.class
    )
    @ApiResponses
    @SLog(tag = "修改WxMina", value = "${args[0].id}")
    @SaCheckLogin
    public Result<?> update(@Param("..") WxMina wxMina, HttpServletRequest req) {
        wxMina.setUpdatedBy(SecurityUtil.getUserId());
        wxMinaService.updateIgnoreNull(wxMina);
        return Result.success();
    }

    @At("/detail")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取WxMina")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SaCheckLogin
    public Result<?> getData(@Param("id") String id, HttpServletRequest req) {
        WxMina wxMina = wxMinaService.fetch(id);
        if (wxMina == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxMina);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除WxMina")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除WxMina", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        WxMina wxMina = wxMinaService.fetch(id);
        if (wxMina == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxMinaService.delete(id);
        req.setAttribute("_slog_msg", wxMina.getId());
        return Result.success();
    }
}
